package com.rbtnet.ssc;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import com.alibaba.fastjson.JSONObject;

import javazoom.jl.player.Player;

/**
 * 
 * @author 1136009436@qq.com
 *
 */
public class Client {
	
	private String accout;  
    private String password;  
    private static int step = 2;
    HttpClient client = HttpClients.createDefault();
      
    public Client(String accout, String password) {  
        super();  
        this.accout = accout;  
        this.password = password;  
    }  
    
    public HttpClient login() {  
        HttpGet getLoginPage = new HttpGet("http://999.cc88866.cc/Mobile_Login#login");//教务处登陆页面get  
          
        try {  
            client.execute(getLoginPage);  
            //获取验证码  
            getVerifyingCode(client);  
              
            //提醒用户并输入验证码  
            System.out.println("请输入验证码");  
            String code;  
            Scanner in = new Scanner(System.in);  
            code = in.nextLine();  
            
            System.out.println("请输入跳过次数");  
            step = in.nextInt();
            in.close();  
              
            //设定post参数，和上图中的内容一致  
            ArrayList<NameValuePair> postData = new ArrayList<NameValuePair>();  
            postData.add(new BasicNameValuePair("login", accout));
            postData.add(new BasicNameValuePair("pass", password));
            postData.add(new BasicNameValuePair("authnum", code));
              
            HttpPost post = new HttpPost("http://999.cc88866.cc/Mobile_Login/Login");//构建post对象  
            post.setEntity(new UrlEncodedFormEntity(postData));//捆绑参数  
            HttpResponse response = client.execute(post);//执行登陆行为  
            EntityUtils.toString(response.getEntity(), "utf-8");  
            System.out.println("登录成功");  
            return client;
        } catch (ClientProtocolException e) {  
            System.out.println(e.getMessage());  
        } catch (IOException e) {  
            System.out.println(e.getMessage());  
        }  
        return null;
    }  
      
    void getVerifyingCode(HttpClient client) {  
        HttpGet getVerifyCode = new HttpGet("http://999.cc88866.cc/Mobile_Login/authnum");//验证码get  
        FileOutputStream fileOutputStream = null;  
        HttpResponse response;  
        try {  
            response = client.execute(getVerifyCode);//获取验证码  
            /*验证码写入文件,当前工程的根目录,保存为verifyCode.jped*/  
            fileOutputStream = new FileOutputStream(new File("verifyCode.png"));  
            response.getEntity().writeTo(fileOutputStream);  
        } catch (ClientProtocolException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                fileOutputStream.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
    }  
    
    public static void main(String[] args) {  
    	Scanner in = new Scanner(System.in);  
    	System.out.println("用户名：");
        String userName = in.nextLine();  
        System.out.println("密码：");
        String password = in.nextLine();  
        
        Client client = new Client(userName, password);  
        HttpClient httpClient = client.login();  
        Double balance = 0.0;
        int count = 0;
        while(true){
        	String lottyid = getCountdown(httpClient);
            String adv = getAdv(httpClient);
            //getFfc(httpClient);
            String ffc = getFfc();
            String data = "[\"37|1|112345|1|110|5|10|,,,,"+ffc+"\"]"; // 5角
            //String data = "[\"37|1|112345|1|110|5|100|,,,,"+ffc+"\"]";  5分
            if(balance < getUserBalance(httpClient) && count == 0){
            	doBet(httpClient, lottyid, data, adv);
            }else{
            	count ++;
            	if(count == step){
            		count = 0;
            		doBet(httpClient, lottyid, data, adv);
            	}else{
            		System.out.println("不下注");
            	}
            }
            balance = getUserBalance(httpClient);
            System.out.println("余额："+balance);
            try {
				Thread.sleep(1000*60);
			} catch (Exception e) {
				e.printStackTrace();
			}
        }
    }  
    
    /**
     * 获取下单参数
     */
    public static String getCountdown(HttpClient client){
    	String url = "http://999.cc88866.cc/LobbyOp/getCountdown/37";
    	HttpGet getCountdown = new HttpGet(url);
    	try {
			HttpResponse response = client.execute(getCountdown);
			String entity = EntityUtils.toString(response.getEntity(), "utf-8");
			Object data = JSONObject.parseObject(entity).get("data");
			Object now = JSONObject.parseObject(data.toString()).get("now");
			//Object next = JSONObject.parseObject(data.toString()).get("next");
			String lottyid = JSONObject.parseObject(now.toString()).get("lottyid").toString();
			String round = JSONObject.parseObject(now.toString()).get("round").toString();
			System.out.println("第"+round+"期");
			return lottyid;
    	} catch (Exception e) {
			e.printStackTrace();
		} 
    	return null;
    }
    
    /**
     * 获取订单号
     */
    public static String getAdv(HttpClient client){
    	String url = "http://999.cc88866.cc/DoBets/__Advisory";
    	HttpGet getAdv = new HttpGet(url);
    	try {
			HttpResponse response = client.execute(getAdv);
			String entity = EntityUtils.toString(response.getEntity(), "utf-8");
			Object data = JSONObject.parseObject(entity).get("data");
			String adv = JSONObject.parseObject(data.toString()).get("adv").toString();
			return adv;
    	} catch (Exception e) {
			e.printStackTrace();
		}
    	return null;
    }
    
    /**
     * 下单
     */
    public static void doBet(HttpClient client,String lottyid,String data,String adv){
    	String url = "http://999.cc88866.cc/DoBets/index/37";
        ArrayList<NameValuePair> postData = new ArrayList<NameValuePair>();  
        postData.add(new BasicNameValuePair("lottyid", lottyid));
        postData.add(new BasicNameValuePair("data", data));  
        postData.add(new BasicNameValuePair("adv", adv));  
        
        HttpPost post = new HttpPost(url);
        try {
			post.setEntity(new UrlEncodedFormEntity(postData));
			HttpResponse response = client.execute(post);
			EntityUtils.toString(response.getEntity(), "utf-8");  
			System.out.println("下单成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    /**
     * 获取分分彩计划
     */
    public static void getFfc(HttpClient client){
    	String url = "http://774444.cc/json/ffc.json";
    	HttpGet getFfc = new HttpGet(url);
    	try {
			HttpResponse response = client.execute(getFfc);
			String entity = EntityUtils.toString(response.getEntity());
			System.out.println(entity);
    	} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    /**
     * 查询余额
     */
    public static Double getUserBalance(HttpClient client){
    	String url = "http://999.cc88866.cc/Common/getUserBalance";
    	HttpGet getUserBalance = new HttpGet(url);
    	try {
			HttpResponse response = client.execute(getUserBalance);
			String entity = EntityUtils.toString(response.getEntity());
			Object data = JSONObject.parseObject(entity).get("data");
			String balance = JSONObject.parseObject(data.toString()).get("balance").toString();
			if(Double.parseDouble(balance) < 10){
				play("dataDancing.mp3");
				System.exit(1);
			}
			return Double.parseDouble(balance);
    	} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
    }
    
    /**
     * 播放声音
     * @param fileName
     */
    public static void play(String fileName) {
        try {
            BufferedInputStream buffer = new BufferedInputStream(
                    new FileInputStream(fileName));
            Player player = new Player(buffer);
            player.play();
        } catch (Exception e) {
            System.out.println(e);
        }
 
    }
    
    /** 
	 * 随机选取
	 *  
	 * 获取0~9其中一个数 
	 *  
	 */  
	private static int randomOneValue() {  
	    int randomValue = (int) (Math.random() * 10);  
	    return randomValue;  
	}  
	
	/**
	 * 获取分分彩
	 * @return
	 */
	private static String getFfc(){
        String ffc = "";
        while(ffc.length()<5){
        	int random = randomOneValue();
        	if(ffc.indexOf(String.valueOf(random)) < 0){
        		ffc += random;
        	}
        }
        System.out.println(ffc);
		return ffc;
	}
}
